package com.tanger.common.excel;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.*;

/**
 *
 * 解析excel文件
 * @Author Hchenbin
 *
 */
public class ReadExcelFile {

	private static final Logger log = LoggerFactory.getLogger(ReadExcelFile.class);

    /**
     * 解析excel
     *
     * @param input 文件流
     * @param isE2007 是否为2007版本的excel
     * @param fromIndex 表单索引
     * @param _row 有几列数据
     * @return
     */
	public static List<Map<Integer, Object>> readExcel(InputStream input, boolean isE2007, int fromIndex, int _row){

        List<Map<Integer, Object>> list = new ArrayList<>();
        Map<Integer, Object> m = null;
        try {
            Workbook wb  = null;
            //根据文件格式(2003或者2007)来初始化
            if(isE2007)
                wb = new XSSFWorkbook(input);
            else
                wb = new HSSFWorkbook(input);
            Sheet sheet = wb.getSheetAt(fromIndex);//获得第一个表单
            Iterator<Row> rows = sheet.rowIterator(); //获得第一个表单的迭代器
            while (rows.hasNext()) {
                m = new HashMap<>();
                Row row = rows.next();  //获得行数据
                if(isE2007){
                    for(int k = 0; k < _row; k++){
                         CellValue cell = new XSSFFormulaEvaluator((XSSFWorkbook)wb).evaluate(row.getCell(k));
                        if(cell == null) continue;
                         switch (cell.getCellType()) {   //根据cell中的类型来输出数据
                             case HSSFCell.CELL_TYPE_NUMERIC:
                                 m.put(k, String.valueOf(new Double(cell.getNumberValue()).longValue()));
                                 break;
                             case HSSFCell.CELL_TYPE_STRING:
                                 m.put(k, cell.getStringValue());
                                 break;
                             case HSSFCell.CELL_TYPE_BOOLEAN:
                                 break;
                             case HSSFCell.CELL_TYPE_FORMULA:
                                 break;
                             default:
                                 break;
                         }
                     }
                }else{
                    for(int k = 0; k < _row; k++) {
                        CellValue cell = new HSSFFormulaEvaluator((HSSFWorkbook)wb).evaluate(row.getCell(k));
                        if(cell == null) continue;
                        switch (cell.getCellType()) {   //根据cell中的类型来输出数据
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                m.put(k, String.valueOf(new Double(cell.getNumberValue()).longValue()));
                                break;
                            case HSSFCell.CELL_TYPE_STRING:
                                m.put(k, cell.getStringValue());
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                break;
                            case HSSFCell.CELL_TYPE_FORMULA:
                                break;
                            default:
                                break;
                        }
                    }
                }
                list.add(m);
            }
        } catch (Exception ex) {
            log.error("=================解析excel文件出错：", ex);
            return null;
        }
        return list;
    }
}